package com.example.mobifin_busticket.views.dialogs;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.views.activities.SearchBusAcitivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class Dialog_SelectDate extends Dialog implements
		android.view.View.OnClickListener {

	DatePicker datePicker;
	Calendar calendar;
	public static int year;
	public static int month;
	public static int day;
	public int MaxYear;
	public int MaxMonth;
	public int MaxDay;
	static final int DATE_PICKER_ID = 999;
	TextView tv;
	StringBuilder date;
	String s;
	Button btn_done;
	static Context mcontext;
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	public Dialog_SelectDate(Activity context) {
		super(context);
		this.mcontext = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.calender_view_activity);
		datePicker = (DatePicker) findViewById(R.id.datePicker1);
		btn_done = (Button) findViewById(R.id.btn_done);
		btn_done.setOnClickListener(this);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == btn_done) {

			year = datePicker.getYear();
			month = datePicker.getMonth();
			day = datePicker.getDayOfMonth();

			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month, day);
			SimpleDateFormat sdf = new SimpleDateFormat("EEE,dd MMM yyyy");
			String dateString = sdf.format(calendar.getTime());
			if (checkIfEndDateIsAfter() == true) {

				SearchBusAcitivity.formateDate = formate_date();
				SearchBusAcitivity.txt_jorny_date.setText(dateString);
				dismiss();
			}

		}

	}

	public String formate_date() {
		year = datePicker.getYear();
		month = datePicker.getMonth();
		day = datePicker.getDayOfMonth();

		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		java.util.Date d = calendar.getTime();

		String formateString = sdf.format(d);

		return formateString;
	}

	public boolean checkIfEndDateIsAfter() {
		boolean flag = true;
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.set(year, month, day);
			java.util.Date d = calendar.getTime();
			if (checkDateValidity(d) == false) {

				flag = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;

	}

	protected boolean checkDateValidity(java.util.Date d) {
		Calendar cal = new GregorianCalendar(d.getYear() + 1900, d.getMonth(),
				d.getDate(), d.getHours(), d.getMinutes());
		if ((cal.getTimeInMillis() - System.currentTimeMillis()) <= 0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(mcontext);
			builder.setTitle("Validating Date");
			builder.setPositiveButton("Ok", new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			builder.setMessage("Please select valid date of journy");
			builder.show();
			return false;
		} else {
			return true;
		}
	}
}